package com.ruoyi.equipment.controller;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.equipment.domain.LgCamera;
import com.ruoyi.equipment.domain.LgSubCold;
import com.ruoyi.equipment.service.LgCameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Author: lek
 * @Description: TODO
 * @Date: 2025/3/7 14:54
 * @Version: 1.0
 */
@RestController
@RequestMapping("/camera")
public class LgCameraController extends BaseController {
    @Autowired
    private LgCameraService lgCameraService;
    /**
     * @author maisan
     * @description //TODO 查询摄像头列表
     * @createTime  2025/3/7 19:07
     * @param
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     **/

    @GetMapping("/list")
    @RequiresPermissions("equipment:camera:list")
    public TableDataInfo list(LgCamera lgCamera){
        startPage();
        lgCamera.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<LgCamera> list = lgCameraService.selectLgCameraList(lgCamera);
        return getDataTable(list);
    }

    /**
     * @author maisan
     * @description //TODO 新增
     * @createTime  2025/3/7 19:08
     * @param
     * @return
     **/
    @PostMapping
    @RequiresPermissions("equipment:camera:add")
    public AjaxResult add(@Validated @RequestBody LgCamera lgCamera){
        lgCamera.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        int i = lgCameraService.insertLgCamera(lgCamera);
        if (i == 0){
            return AjaxResult.error("新增失败");
        }
        return AjaxResult.success();
    }

    /**
     * @author maisan
     * @description //TODO 根据id查询
     * @createTime  2025/3/7 19:40
     * @param
     * @return
     **/
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Integer id){
        LgCamera lgCamera = lgCameraService.selectLgCameraById(id);
        return AjaxResult.success(lgCamera);
    }

    /**
     * @author maisan
     * @description //TODO 更新
     * @createTime  2025/3/7 19:42
     * @param
     * @return
     **/
    @PutMapping
    @RequiresPermissions("equipment:camera:edit")
    public AjaxResult update(@Validated @RequestBody LgCamera lgCamera){
        int i = lgCameraService.updateLgCamera(lgCamera);
        if (i == 0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }


    /**
     * @author maisan
     * @description //TODO 删除
     * @createTime  2025/3/7 19:38
     * @param
     * @return
     **/
    @DeleteMapping("/{ids}")
    @RequiresPermissions("equipment:camera:remove")
    public AjaxResult remove(@PathVariable Integer[] ids){
        int i = lgCameraService.deleteLgCameraById(ids);
        if (i == 0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }
    /**
     * @author maisan
     * @description 修改绑定信息
     * @createTime  2025/3/8 10:58
     * @param
     * @return
     **/

    @PutMapping("/updateSmallCold")
    @RequiresPermissions("equipment:camera:updateVehicle")
    public AjaxResult updateSmallCold(@RequestBody Map<String, Object> map){
        System.out.println(map.get("cid"));
        System.out.println(map.get("subId"));
        System.out.println(map.get("updateBy"));
        LgSubCold lgSubCold = lgCameraService.selectSubColdById((Integer) map.get("subId"));
        LgCamera LgCamera = new LgCamera();
        LgCamera.setLgSubCold(lgSubCold);
        List<LgCamera> lgCameraList = lgCameraService.selectLgCameraList(LgCamera);
        if (lgCameraList.size() == 0){
            LgCamera.setId((Integer) map.get("cid"));
            LgCamera.setColdStorageId((Integer) map.get("subId"));
            return AjaxResult.success(lgCameraService.updateLgCamera(LgCamera));
        }
        return AjaxResult.error("该设备已绑定");
    }

    /**
     * @author maisan
     * @description //TODO 取消绑定冷库
     * @createTime  2025/3/8 14:31
     * @param
     * @return
     **/
    @PutMapping("/delBound")
    @RequiresPermissions("equipment:camera:delBound")
    public AjaxResult delBound(@RequestBody Map<String, Object> map){
        LgCamera LgCamera = lgCameraService.selectLgCameraById((Integer) map.get("id"));
        LgCamera.setColdStorageId(0);
        return AjaxResult.success(lgCameraService.updateLgCamera(LgCamera));
    }

}
